<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Profile extends CI_Controller {

    function __construct() {
        parent::__construct();
         $this->load->helper('utilidades');
          $this->load->helper("sessiones");
        /* cargo librerias necesarias */
        $this->load->helper('url');
        $this->load->database();
        $this->load->library('session');
        $this->load->library('pagination');
        $this->load->model('user');
        validaSession($this->session->userdata('user_id'));
    }

    function view() {
       
        $this->load->view('home/header.php');
        $this->load->helper('utilidades');
  
        $data["user_id"] = $this->session->userdata('user_id');
        
        $this->load->view('profile/index.php', $data);
        $this->load->view('home/footer.php');
    }

    function edit($id) {
        $this->load->view('home/header.php');
        $this->load->view('profile/edit.php', $id);
        $this->load->view('home/footer.php');
    }

    function save() {
        $id = $this->input->post('id');
        $nombre = $this->input->post('nombre');
        $apellido = $this->input->post('apellido');
        $email = $this->input->post('email');
        $telefono = $this->input->post('telefono');

        $sql = "UPDATE users SET nombre='$nombre', apellido='$apellido', email='$email', telefono='$telefono' WHERE id = $id";
        $res = $this->db->query($sql);
        if ($res) {
            //$this->do_upload();
            redirect('profile/view/' . $id, 'refresh');
        } else {
            redirect('view/' . $id, 'refresh');
        }
    }

    function do_upload() {

        $config['upload_path'] = './public/images/avatars/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '1000';
        $config['max_width'] = '1024';
        $config['max_height'] = '768';

        $this->load->library('upload', $config);

        if (!$this->upload->do_upload()) {
            $error = array('error' => $this->upload->display_errors());
            print_r($error);die;
        }
    }

    function cursos() {
       
        $this->load->library('pagination');
        $user_id = $this->session->userdata('user_id');

        $opciones = array();
        $desde = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;

        $opciones['per_page'] = 5;
        $opciones['base_url'] = base_url() . 'profile/cursos';
        $opciones['total_rows'] = $this->user->getNumCursosUser($user_id);
        $opciones['uri_segment'] = 3;
        $opciones['use_page_numbers'] = true;
        $opciones['full_tag_open'] = '<div class="pagination pagination-small pagination-centered"><ul>';
        $opciones['full_tag_close'] = '</ul></div>';
        $opciones['prev_link'] = '&lt;&lt;';
        $opciones['prev_tag_open'] = '<li>';
        $opciones['prev_tag_close'] = '</li>';
        $opciones['next_link'] = '&gt;&gt;';
        $opciones['next_tag_open'] = '<li>';
        $opciones['next_tag_close'] = '</li>';
        $opciones['cur_tag_open'] = '<li class="active"><a href="#">';
        $opciones['cur_tag_close'] = '</a></li>';
        $opciones['num_tag_open'] = '<li>';
        $opciones['num_tag_close'] = '</li>';
        $opciones['first_link'] = True;
        $opciones['last_link'] = true;
        $opciones['display_pages'] = true;

        $this->pagination->initialize($opciones);

        $data['lista'] = $this->user->getTodosCursos($opciones['per_page'], $desde);
        $data['paginacion'] = $this->pagination->create_links();

        $this->load->view('home/header.php');
        $this->load->view('profile/cursos.php', $data);
        $this->load->view('home/footer.php');
    }
    function consultas($id_curso =null){
        $this->load->library('pagination');
         $this->load->model('institute');
        $user_id = $this->session->userdata('user_id');
        
        $opciones = array();
        $desde = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
        
        $opciones['per_page'] = 5;
        $opciones['base_url'] = base_url() . 'profile/cursos';
        $opciones['total_rows'] = $this->institute->getConsultasInstituto($id_curso,true);
        $opciones['uri_segment'] = 3;
        $opciones['use_page_numbers'] = true;
        $opciones['full_tag_open'] = '<div class="pagination pagination-small pagination-centered"><ul>';
        $opciones['full_tag_close'] = '</ul></div>';
        $opciones['prev_link'] = '&lt   ;&lt;';
        $opciones['prev_tag_open'] = '<li>';
        $opciones['prev_tag_close'] = '</li>';
        $opciones['next_link'] = '&gt;&gt;';
        $opciones['next_tag_open'] = '<li>';
        $opciones['next_tag_close'] = '</li>';
        $opciones['cur_tag_open'] = '<li class="active"><a href="#">';
        $opciones['cur_tag_close'] = '</a></li>';
        $opciones['num_tag_open'] = '<li>';
        $opciones['num_tag_close'] = '</li>';
        $opciones['first_link'] = True;
        $opciones['last_link'] = true;
        $opciones['display_pages'] = true;

        $this->pagination->initialize($opciones);

        $data['lista'] = $this->institute->getConsultasInstituto($id_curso);
        $data['paginacion'] = $this->pagination->create_links();
         $data['cursos'] = $this->institute->getConsultasInstituto($id_curso);
        $this->load->view('home/header.php');
        $this->load->view('profile/consultas.php', $data);
        $this->load->view('home/footer.php');  
        
    }
}

?>